package com.sz.app.customer.api.trade;

import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.project.service.ProjectService;
import com.sz.biz.common.platform.service.PlatformCompanyService;
import com.sz.biz.trade.bill.service.TrdBillingOrderRelService;
import com.sz.biz.trade.bill.service.TrdBillingService;
import com.sz.common.base.dto.CommonModel;
import com.sz.common.base.dto.ResultDto;
import com.sz.biz.logistics.bill.entity.BillingStatus;
import com.sz.biz.logistics.bill.dto.BillingUrlDto;
import com.sz.common.core.service.PrincipalUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Function: 客户贸易账单管理 <br>
 * Author: changwentao <br>
 * Date: 2017-07-20 14:18:00
 */
@RestController
@RequestMapping("${api}/cust/trd/cus/financial")
@Api(description = " ", tags = "042--001、客户贸易账单管理")
public class CusTrdBillingController extends BaseController {

    @Autowired
    private TrdBillingService trdBillingService;
    @Autowired
    private TrdBillingOrderRelService trdBillingOrderRelService;
    @Autowired
    private PlatformCompanyService platformCompanyService;

    /**
     * 分页查询账单列表
     */
    @ApiOperation(value = "搜索账单", notes = "搜索账单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billNo", value = "账单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "platformId", value = "平台id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startEffectTime", value = "起始出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endEffectTime", value = "结束出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultDto findBillingList(
            @RequestParam(required = false) String billNo,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) Integer platformId,
            @RequestParam(required = false) BillingStatus status,
            @RequestParam(required = false) String startEffectTime,
            @RequestParam(required = false) String endEffectTime,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            @RequestParam(required = false) String sort,
            @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        int loginUserId = PrincipalUtils.getAccountId();
        return trdBillingService.findByFilterForCustomer(platformId, billNo, orderNo, loginUserId, status,
                false,
                startEffectTime, endEffectTime, pageIndex, pageSize, sort, asc);
    }

    @ApiOperation(value = "获取账单下所有订单", notes = "获取账单下所有订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billNo", value = "账单号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "custUserId", value = "账号id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "goodsName", value = "搜索的商品名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startEffectTime", value = "起始出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endEffectTime", value = "结束出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/orders", method = {RequestMethod.GET})
    public ResultDto getOrdersByBillNo(
            @RequestParam String billNo,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) String goodsName,
            @RequestParam(required = false) Integer custUserId,
            @RequestParam(required = false) String startEffectTime,
            @RequestParam(required = false) String endEffectTime,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            @RequestParam(required = false) String sort,
            @RequestParam(required = false, defaultValue = "true") Boolean asc) {

        return trdBillingOrderRelService.getOrdersByBillNoForCustomer(1, billNo, orderNo, goodsName,
                custUserId,
                startEffectTime, endEffectTime, pageIndex, pageSize, sort, asc);
    }

    /**
     * 所有账单状态
     *
     * @return ResultDto
     */
    @ApiOperation(value = "所有账单状态", notes = "所有账单状态")
    @RequestMapping(value = "/billingStatus", method = RequestMethod.GET)
    public ResultDto getBillingStatus() {
        List<CommonModel> list = trdBillingService.getBillingStatusForCustomer();
        ResultDto result = new ResultDto();
        result.setData(list);
        return result;
    }

    /**
     * 账单导出
     */
    @ApiOperation(value = "账单导出", notes = "账单导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billNos", value = "账单号集合", dataType = "List", paramType = "body", required = true)
    })
    @RequestMapping(value = "/export", method = {RequestMethod.POST})
    public ResultDto export(@RequestBody List<String> billNos) throws ParseException {
        List<BillingUrlDto> list = trdBillingService.exportCustomer(billNos);
        ResultDto result = new ResultDto();
        result.setData(list);
        return result;
    }

    /**
     * 根据billNo审批流程
     *
     * @param billNo billNo
     * @return 查询到的审批流程
     */
    @RequestMapping(value = "/approval/{billNo}", method = RequestMethod.GET)
    @ApiOperation(value = "查询审批流程", notes = "根据账单号获取审批流程")
    public ResultDto findFinancialApprovals(@ApiParam(value = "billNo", required = true) @PathVariable String billNo) {
        List<Map<String, String>> approvals = trdBillingService.findFinancialApprovals(billNo);
        ResultDto result = new ResultDto();
        result.setData(approvals);
        return result;
    }

    @Autowired
    private ProjectService projectService;

    @ApiOperation(value = "获取平台公司", notes = "平台公司")
    @RequestMapping(value = "/platcompanys", method = {RequestMethod.GET})
    public ResultDto getPlatCompanys() {
        return new ResultDto(platformCompanyService.findAll());
    }
}
